package com.bentley.kotlindemo.ui.kits.home

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.bentley.kotlindemo.App
import com.bentley.kotlindemo.R
import com.bentley.kotlindemo.base.BaseActivity
import com.bentley.kotlindemo.databinding.ActivityHomeBinding
import com.bentley.kotlindemo.db.AppDatabase
import com.bentley.kotlindemo.db.entity.Author
import com.bentley.kotlindemo.db.entity.Teacher
import com.bentley.kotlindemo.net.RespStatus
import com.bentley.kotlindemo.ui.kits.authorlist.AuthorListActivity
import com.bentley.kotlindemo.ui.kits.authorlist_paging.AuthorPagingActivity
import com.bentley.kotlindemo.ui.kits.authorlist_paging.AuthorPagingModel
import com.bentley.kotlindemo.ui.kits.bottomtab.BottomTabActivity
import com.bentley.kotlindemo.ui.kits.lefttab.LeftTabActivity
import com.bentley.kotlindemo.ui.kits.toptab.ToptabActivity
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * <pre>
 *  ┌────────────────────────────────────────────
 *  │ @author  Lee  @date 2021/3/16 11:24
 *  ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
 *  │ @desc
 *  └────────────────────────────────────────────
 * </pre>
 */

class HomeActivity : BaseActivity<ActivityHomeBinding,HomeViewModel>() {

    override fun getLayoutId()  = R.layout.activity_home

     var authorViewModel:AuthorPagingModel ? =   null
    private fun setupObservers() {
        viewModel.getUsers().observe(this, Observer {
            it?.let { resource ->
                when (resource.status) {
                    RespStatus.SUCCESS -> {
                        Toast.makeText(application, "success ", Toast.LENGTH_SHORT).show()
                        loading?.dismiss()
                    }
                    RespStatus.ERROR -> {
                        Toast.makeText(this, "error", Toast.LENGTH_LONG).show()
                        loading?.dismiss()
                    }
                    RespStatus.LOADING -> {
                        Toast.makeText(application, "loading ", Toast.LENGTH_SHORT).show()
                        loading?.show()
                    }
                }
            }
        })

    }

    override  fun initView(savedInstanceState: Bundle?) {
       authorViewModel =  ViewModelProvider(this).get(AuthorPagingModel::class.java);
        authorViewModel!!.name.postValue("我是首页传递的值")
//        StatusBarUtils.setStatusBarColor(this, Color.WHITE)
//        StatusBarUtils.setStatusBarLightMode(this,true)

        dataBinding.networkBtn.setOnClickListener {
            setupObservers()
        }

        dataBinding.listBtn.setOnClickListener {
            startActivity(Intent(this, AuthorListActivity::class.java))
        }

        dataBinding.pagingBtn.setOnClickListener {
            startActivity(Intent(this, AuthorPagingActivity::class.java))
        }

        dataBinding.topTab.setOnClickListener {
            startActivity(Intent(this, ToptabActivity::class.java))
        }

        dataBinding.bottomTab.setOnClickListener {
            startActivity(Intent(this, BottomTabActivity::class.java))
        }

        dataBinding.leftTab.setOnClickListener {
            startActivity(Intent(this, LeftTabActivity::class.java))
        }
//        DialogUtil.getWaitDialog(this,"测试中...").show()
//
//
        GlobalScope.launch {

            var auth = Author("1004","张飞","22")
            var auth1 = Author("1002","赵云","33")
            var auth2 = Author("1003","马超","66")
            var list = ArrayList<Author>()
            list.add(auth)
            list.add(auth1)
            list.add(auth2)
            AppDatabase.getInstance(App.INSTANCE).plantDao().insertAll(list)

            var ll = AppDatabase.getInstance(App.INSTANCE).plantDao().getPlants()
            val aa = 2

            var teacher = Teacher("1005","王老师","28")
            var teacherlist = ArrayList<Teacher>()
            teacherlist.add(teacher)
            AppDatabase.getInstance(App.INSTANCE).teacherDao().insertAll(teacherlist)
        }





        //KOTLIN PRACTISE
        val getLenth : (String)-> Int = {inputString ->
          inputString.length
        }

        var ss = getLenth("ss")

        stringMapper("Android", { input ->
            input.length
        })
    }

    fun stringMapper(str: String, mapper: (String) -> Int): Int {
        // Invoke function
        return mapper(str)
    }

}